/*----------------------------------------------------------------------------
 * Name:    Serial.c
 * Purpose: Low Level Serial Routines
 * Note(s): possible defines select the used communication interface:
 *            __DBG_ITM   - ITM SWO interface
 *                        - USART1 interface  (default)
 *----------------------------------------------------------------------------
 * This file is part of the uVision/ARM development tools.
 * This software may only be used under the terms of a valid, current,
 * end user licence from KEIL for a compatible version of KEIL software
 * development tools. Nothing else gives you the right to use this software.
 *
 * This software is supplied "AS IS" without warranties of any kind.
 *
 * Copyright (c) 2012 Keil - An ARM Company. All rights reserved.
 *----------------------------------------------------------------------------*/

#include "mhscpu.h"

#ifdef __DBG_ITM
volatile int ITM_RxBuffer = ITM_RXBUFFER_EMPTY;  /*  CMSIS Debug Input        */
#endif

UART_TypeDef *PRINT_UART = UART0;

/*----------------------------------------------------------------------------
  Initialize UART pins, Baudrate
 *----------------------------------------------------------------------------*/
void SER_Init (void) {
#ifndef __DBG_ITM

	UART_InitTypeDef UART_InitStructure;

	if (UART0 == PRINT_UART)
	{
        SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0,DISABLE);
        SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART0,ENABLE);
        SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART0, ENABLE);
	} 
	else if (UART1 == PRINT_UART)
	{
		SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART1,DISABLE);
        SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_UART1,ENABLE);
        SYSCTRL_APBPeriphResetCmd(SYSCTRL_APBPeriph_UART1, ENABLE);
	} 
	
	UART_StructInit(&UART_InitStructure);
	
	UART_InitStructure.UART_BaudRate = 9600;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	/*  enable USART  */
	UART_Init(PRINT_UART, &UART_InitStructure);

#endif
}


/*----------------------------------------------------------------------------
  Write character to Serial Port
 *----------------------------------------------------------------------------*/
int SER_PutChar (int c) {

#ifdef __DBG_ITM
    ITM_SendChar(c);
#else
	while(!(PRINT_UART->LSR & 0x20));
	return (PRINT_UART->OFFSET_0.THR = (c & 0x1FF));
#endif
  return (c);
}


/*----------------------------------------------------------------------------
  Read character from Serial Port   (blocking read)
 *----------------------------------------------------------------------------*/
int SER_GetChar (void) {

#ifdef __DBG_ITM
  while (ITM_CheckChar() != 1) __NOP();
  return (ITM_ReceiveChar());
#else
	while(PRINT_UART->LSR & 0x01);
	return (PRINT_UART->OFFSET_0.RBR & 0xFF);
#endif
}


/*----------------------------------------------------------------------------
  Check if a character is received
 *----------------------------------------------------------------------------*/
int SER_CheckChar (void) {

#ifdef __DBG_ITM
  return (ITM_CheckChar());
#else
  if (PRINT_UART->LSR & 0x01)
    return (1);
  else
    return (0);
#endif
}
